<template>
  <el-dialog title="业务流程发布管理" :visible.sync="isVisiable" width="600" :close-on-click-modal="false" @close="cacel()">
    <el-table :data="dataList" stripe>
      <el-table-column label="序号" header-align="center" align="center" :formatter="nShowIndex" width="80"></el-table-column>
      <el-table-column prop="flowEdition" label="流程版本" header-align="center" align="center">
      </el-table-column>
      <el-table-column prop="releaseTime" :formatter="formater" label="发布时间" header-align="center" align="center"
                       width="150">
      </el-table-column>
    </el-table>
    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
                     :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background
                     layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
    <div slot="footer">
      <span>
        <el-button type="primary" @click="save()">发布流程</el-button>
        <el-button type="primary" plain @click="isVisiable=false">取消</el-button>
      </span>
    </div>
  </el-dialog>
</template>

<script>
import {getObj, putObj} from '@/api/workflow/wfflowconfig'
import {fetchList, addObj} from '@/api/workflow/wfflowrelease'
import {Message} from "element-ui";

export default {
  components: {},
  data() {
    var that = this;
    return {
      isVisiable: false,
      mainInfo: {},
      configInfomainInfo: {},
      configInfo: "",
      sm: "",
      dataForm: {
        flowId: 0
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      //线路配置
      flowConfigLine: [],
      //节点配置
      flowConfigNode: [],
      //分支配置
      flowConfigGateway: [],
      //未配置的节点
      tasks: 0,
      //未配置的分支
      Gateways: 0,
    }
  },
  created: function () {
  },
  mounted() {
  },
  methods: {
    formater(row, column, cellValue, index) {
      if (cellValue) {
        return this.$moment(cellValue).format("yyyy-MM-DD HH:mm");
      } else {
        return cellValue;
      }
    },
    getDataList() {
      fetchList(Object.assign({
        page: this.pageIndex,
        size: this.pageSize
      }, this.dataForm)).then(response => {
        this.dataList = response.data.data.records
        this.totalPage = response.data.data.total
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    nShowIndex(row, column, cellValue, index) {
      return index + 1 + this.pageSize*(this.pageIndex-1);
    },
    init(id) {
      this.mainInfo = {};
      this.configInfo = "";
      getObj(id)
        .then(res => {
          if (res.data.code == "0") {
            this.mainInfo = res.data.data;
            this.isVisiable = true;
            this.dataForm.flowId = id;
            this.$nextTick(() => {
              this.$nextTick(() => {
                this.getDataList();
              });
            });
            //未配置的节点
            this.tasks = 0;
            //未配置的分支
            this.Gateways = 0;
            //线路配置
            this.flowConfigLine = JSON.parse(this.mainInfo.flowConfigLine);
            //分支配置
            if (this.mainInfo.flowConfigGateway == "") {
              this.mainInfo.flowConfigGateway = null;
            }
            this.flowConfigGateway = JSON.parse(this.mainInfo.flowConfigGateway);
            //节点配置
            this.flowConfigNode = JSON.parse(this.mainInfo.flowConfigNode);
            for (var i in this.flowConfigLine.Task) {
              var ids = this.flowConfigLine.Task[i];
              if (this.flowConfigNode != null) {
                if (this.flowConfigNode[ids] != undefined) {

                } else {
                  this.tasks++;
                }
              } else {
                this.tasks++;
              }
            }
            for (var i in this.flowConfigLine.Gateway) {
              var ids = this.flowConfigLine.Gateway[i];
              if (this.flowConfigLine[ids].types == "complexGateway" || this.flowConfigLine[ids].types == "parallelGateway") {

              } else {
                if (this.flowConfigGateway != null) {
                  if (this.flowConfigGateway[ids] != undefined) {
                  } else {
                    this.Gateways++;
                  }
                } else {
                  this.Gateways++;
                }

              }
            }

          } else {
            this.$message({
              type: 'error',
              message: res.msg
            });
          }
        }).catch(() => {
        this.$message({
          type: 'info',
          message: '操作错误'
        });
      });
    },
    save() {
      if (this.tasks != 0 || this.Gateways != 0) {
        var messages = "";
        if (this.tasks != 0 && this.Gateways != 0) {
          messages = "该流程有" + this.tasks + "个节点," + this.Gateways + "个分支未配置，无法发布...";
        } else if (this.tasks != 0) {
          messages = "该流程有" + this.tasks + "个节点未配置，无法发布...";
        } else {
          messages = "该流程有" + this.Gateways + "个分支未配置,无法发布...";
        }
        Message({
          message: messages,
          type: "error",
          duration: 3 * 1000
        });
        return
      }
      var info = {};
      info.flowId = this.mainInfo.id * 1;
      info.flowName = this.mainInfo.flowName + "";
      info.flowEdition = "ver2.0." + this.$moment(new Date()).format("YYMMDDHHmmss");
      delete this.mainInfo.id;
      delete this.mainInfo.flowName;
      delete this.mainInfo.flowContent;
      delete this.mainInfo.flowEdition;
      delete this.mainInfo.createTime;
      delete this.mainInfo.careteUser;
      delete this.mainInfo.modifyTime;
      delete this.mainInfo.modifyUser;
      delete this.mainInfo.flowState;
      // delete this.mainInfo.sortBusiness;
      delete this.mainInfo.sortStatistics;
      delete this.mainInfo.startRoles;
      delete this.mainInfo.startIcon;
      delete this.mainInfo.flowOrder;
      //流程配置信息验证

      //开始角色获取
      var startRoles = [];
      var flowConfigLine = JSON.parse(this.mainInfo.flowConfigLine);
      var flowConfigNode = JSON.parse(this.mainInfo.flowConfigNode);
      var StartTask = flowConfigLine.StartTask;
      for (var i in StartTask) {
        var roles = flowConfigNode[StartTask[i]].rootRole;
        for (var j in roles) {
          if (startRoles.indexOf(roles[j]) == -1) {
            startRoles.push(roles[j]);
          }
        }
      }
      info.wholeConfig = encodeURI(JSON.stringify(this.mainInfo));
      this.$confirm('此操作将最新的流程配置信息发布版本, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        addObj(info)
          .then(res => {
            if (res.data.code == "0") {
              this.init(info.flowId);
              Message({
                message:"流程配置信息发布版本成功...",
                type: "success",
                duration: 3 * 1000
              });
              putObj({
                  id: info.flowId,
                  flowEdition: info.flowEdition,
                })
                .then(ress => {
                  if (ress.data.code == "0") {
                    Message({
                      message: "流程配置信息更新版本成功...",
                      type: "success",
                      duration: 3 * 1000
                    });
                  } else {
                    Message({
                      message: ress.msg,
                      type: "error",
                      duration: 3 * 1000
                    });
                  }
                }).catch(error => {
               //console.log(error);
              });
            } else {
              Message({
                message: res.msg,
                type: "error",
                duration: 3 * 1000
              });
            }
          }).catch(error => {
         //console.log(error);
        });
      })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          });
        });
    },
    cacel() {
      this.$emit("loadData");
    }
  }
}
</script>

<style>
</style>
